//
// Created by hunan on 2025/10/1.
#include <cstdio>
#include <stdlib.h>
#include <string>
#include "hls_download_task.h"
#include "public_module/log_module/log_public_module.h"

void Usage(const char * name)
{
    LOG_PUBLIC_MODULE("usage: %s -i url -o output_file -p ts_abs_path -n connection_number", name);
}

int main(int argc, char * argv[])
{
    std::string url;
    std::string m3u8_file;
    std::string fragment_path;
    int number = 1;
    char ch = {};
    while ((ch = getopt(argc, argv, "i:p:o:n:h")) != -1)
    {
        switch (ch)
        {
            case 'i':
            {
                url = optarg ? optarg : "";
                break;
            }
            case 'o':
            {
                m3u8_file = optarg ? optarg : "";
            }
            case 'p':
            {
                fragment_path = optarg ? optarg : "";
                break;
            }
            case 'n':
            {
                number = atoi(optarg);
                break;
            }
            case 'h':
            {
                Usage(argv[0]);
                exit(EXIT_SUCCESS);
            }
            default:
            {
                break;
            }
        }
    }
    LOG_UPDATE_MODULE(SOCKET_MODULE_NAME, 0);
    if (!url.empty() && !m3u8_file.empty() && !fragment_path.empty() && number > 0)
    {
        HlsDownloadTask task(url, m3u8_file, fragment_path, number);
        task.StartDownload();
    }
    return EXIT_SUCCESS;
}